http://www.lintcode.com/en/problem/string-permutation-ii/
Given a string, find all permutations of it without duplicates.
Example
Given “abb”, return [“abb”, “bab”, “bba”].Given “aabb”, return [“aabb”, “abab”, “baba”, “bbaa”, “abba”, “baab”].
We can see the keywords in the description of the question is that “find all permutations”. It is easy for us to think that we can use dfs to solve this problem.
1 |
|
This is a navie version of solution. It needs extra memory which is O(n), where the n represents the length of the input string, to store the information if this char in string has been added. The time complexity of this algorithm is O(n^2).
If you want to solve this problem without extra memory space, we can use solution used in “Next Permutation”
1 |
|